from config import base_url,adminSecret
import requests
import random
from loguru import logger
from jsonpath import jsonpath



def initGlobalValue(dict_value=None):
    """
    初始化全局变量
    """
    global extractResult    # 定义全局变量，方便测试用例去公共读取
    extractResult = {}  # 初始化全局变量
    if dict_value != None:
        extractResult.update(dict_value)
        
    

def setGlobalValue(dict_value):
    """
    修改环境变量
    """
    extractResult.update(dict_value)

def getGlobalValue():
    """
    获取环境变量
    """
    return extractResult



def getToken():
    """
    获取后台管理员的token信息
    """
    url = base_url + "/system/login"
    mathod = "post"
    data = adminSecret
    r = requests.request(mathod,url,json=data)
    return r.json()["data"]["token"]



def generatePhone():
    """
    随机生成手机号
    """
    nums = str(random.randint(10000000,99999999))
    return "182" + nums


def replaceValue(text,environment):
    """
    把测试数据中的变量进行替换
    """
    if text in (None,""):
        return text
    for i in environment:
        text = text.replace(i,str(environment[i]))
    try:
        text = eval(text)
    except:
        text = text
    return text


def getJsonPathValue(json,path):
    """
    @功能    :根据jsonpath去获取对应的数据
    @参数    :
    @返回值  :
    @时间    :2023/04/01 22:35:23
    @作者    :浪晋
    @版本    :1.0
    """
    try:
        value = jsonpath(json,path)[0]
    except:
        value = None
    return value